<template>
  <div class="">
    <heads></heads>
    <main>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="食品名称">
                <span>{{ props.row.foodName }}</span>
              </el-form-item>
              <el-form-item label="商店名称">
                <span>{{ props.row.busine.businessName }}</span>
              </el-form-item>
              <el-form-item label="食品ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="店铺ID">
                <span>{{ props.row.busId }}</span>
              </el-form-item>
              <el-form-item label="食品介绍">
                <span>{{ props.row.foodText }}</span>
              </el-form-item>
              <el-form-item label="餐馆地址">
                <span>{{ props.row.busine.businessAddress }}</span>
              </el-form-item>
              <el-form-item label="食品评分">
                <span>{{ props.row.foodScore }}</span>
              </el-form-item>
              <el-form-item label="食品分类">
                <span>{{ props.row.foodClass.foodClassName }}</span>
              </el-form-item>
              <el-form-item label="月销量">
                <span>{{ props.row.foodOrderNum }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="食品名称" prop="foodName"> </el-table-column>
        <el-table-column label="食品介绍" prop="foodText"> </el-table-column>
        <el-table-column label="评分" prop="foodScore"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="updateFoods(scope.row.id)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteFood(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="修改食品信息" :visible.sync="dialogFormVisible">
        <el-form :model="foods">
          <el-form-item label="食品名称">
            <el-input autocomplete="off" v-model="foods.foodName" style="width: 50%"></el-input>
          </el-form-item>
          <el-form-item label="食品介绍">
            <el-input autocomplete="off" v-model="foods.foodText" style="width: 50%"></el-input>
          </el-form-item>
          <el-form-item label="食品分类">
            <el-select v-model="foods.foodClass" placeholder="请选择">
              <el-option v-for="item in optionsClass" :key="item.id" :label="item.foodClassName" :value="item.id"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="食品图片">
            <el-upload action="http://127.0.0.1:8000/admin/Upload/addFile" :on-success="uploadSuccess" style="position: relative" list-type="picture-card" :limit="1" :auto-upload="true">
              <i slot="default" class="el-icon-plus"></i>
              <div>
                <el-image class="el-upload-list__item-thumbnail" style="position: absolute; top: 0; left: 65px; width: 150px; height: 150px" :src="foods.foodImg" fit="fill"></el-image>
              </div>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-table :data="tableDatas" style="width: 100%">
              <el-table-column prop="speciName" label="规格" width="180"> </el-table-column>
              <el-table-column prop="wrapMoney" label="包装费" width="180"> </el-table-column>
              <el-table-column prop="foodMoney" label="价格"> </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="danger" size="mini" @click="deleteDanger(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancelUpdate">取 消</el-button>
          <el-button type="primary" @click="sureUpdate">确 定</el-button>
        </div>
      </el-dialog>
    </main>
    <footer>
      <el-pagination background layout="prev, pager, next" v-on:current-change="changePage" v-on:prev-click="perventPage" v-on:next-click="nextPage" :total="pageNum * 10"> </el-pagination>
    </footer>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import heads from "@/components/adminHead.vue";

import { selectFoods, selectFooClass, selectSpeci, deleteFile, deleteSpeci, updateFoods, deleteFoods } from "@/api/getPostApi";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    heads,
  },
  data() {
    //这里存放数据
    return {
      optionsClass: [],
      tableData: [],
      page: 1,
      pageNum: 0,
      dialogFormVisible: false,
      foods: {
        id: "",
        foodName: "",
        foodClass: "",
        foodText: "",
        foodImg: "",
      },
      newFilePath: "",
      tableDatas: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    updateFoods(id) {
      this.tableData.forEach((val) => {
        if (val.id === id) {
          this.foods.id = val.id;
          this.foods.foodName = val.foodName;
          this.foods.foodText = val.foodText;
          this.foods.foodClass = val.foodClassId;
          this.foods.foodImg = `http://127.0.0.1:8000/${val.foodImg}`;
        }
      });
      this.dialogFormVisible = true;
      selectSpeci(id).then((response) => {
        this.tableDatas = response.data;
      });
    },
    uploadSuccess(val) {
      this.newFilePath = val;
      this.foods.foodImg = val;
    },
    cancelUpdate() {
      if (this.newFilePath) {
        deleteFile(this.newFilePath);
        this.newFilePath = "";
      }
      this.dialogFormVisible = false;
    },
    deleteDanger(id) {
      if (this.tableDatas.length > 1) {
        this.tableDatas = this.tableDatas.filter((val) => val.id !== id);
        deleteSpeci(id);
      }
    },
    sureUpdate() {
      updateFoods(this.foods.id, this.foods.foodName, this.foods.foodText, this.foods.foodClass, this.newFilePath).then((response) => {
        if (response.data.status === 200) {
          this.$message({
            message: "恭喜你，修改成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦，修改失败");
        }
        this.selectTableData();
        this.dialogFormVisible = false;
      });
    },
    selectTableData() {
      selectFoods(this.page).then((response) => {
        this.tableData = response.data.foodsList;
        this.pageNum = Math.ceil(response.data.count / 10);
      });
    },
    perventPage() {
      this.page -= 1;
      this.selectTableData();
    },

    nextPage() {
      this.page += 1;
      this.selectTableData();
    },

    changePage(val) {
      this.page = val;
      this.selectTableData();
    },
    deleteFood(id) {
      this.tableData = this.tableData.filter((val) => val.id !== id);

      deleteFoods(id);
      if (this.tableData <= 0) {
        this.pageNum -= 1;
        this.perventPage();
      }
      this.$message({
        message: "恭喜你，删除成功",
        type: "success",
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.selectTableData();
    selectFooClass().then((response) => {
      this.optionsClass = response.data;
    });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类
@import "@/style/mixin.scss";
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  @include marginWidth;
}

footer {
  @include footer;
}
</style>
